package com.ma.chapter8.executors;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName TestExecutor
 * @Author: mayongqiang
 * @DATE 2022/3/21 19:43
 * @Description: JDK线程池工具类  Executors 测试
 */
/*默认构造器
public ThreadPoolExecutor(int corePoolSize,
                  int maximumPoolSize,
                  long keepAliveTime,
                  TimeUnit unit,
                  BlockingQueue<Runnable> workQueue) * */
@Slf4j(topic = "m.TestExecutor")
public class FixedThreadPool {
    public static void main(String[] args) {


        /* 1、 Executors.newFixedThreadPool(2)
         *  固定大小的线程池 return new ThreadPoolExecutor(nThreads, nThreads,
         *                0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
         *    特点：核心线程数=最大线程数（没有救急线程）无需超时时间 阻塞队列是无界的,可以放下任意数量的任务
         *   评价：适用于任务量已知，相对耗时的任务
         *  */
        ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
            private AtomicInteger i = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "my-pool_t" + i.getAndIncrement());
            }
        });
        pool.execute(() -> {
            log.debug("1");
        });
        pool.execute(() -> {
            log.debug("2");
        });
        pool.execute(() -> {
            log.debug("3");
        });





    }
}
